//
//  ReviewDetailViewController.m
//  CarPark
//
//  Created by oceans on 2019/8/6.
//  Copyright © 2019 oceans. All rights reserved.
//

#import "ReviewDetailViewController.h"
#import "ReviewDetailCollectionViewCell.h"
#import "ReviewDetailHeaderReusableView.h"
#import "CaptureDeviceCollectionViewCell.h"
#import "TipManager.h"

@interface ReviewDetailViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) UICollectionView *collection;
@property (strong, nonatomic) NSDictionary *info;
@property (strong, nonatomic) UIView *beiZhuView;
@property (strong, nonatomic) UITextView *textView;
@end

@implementation ReviewDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"任务详情";
    
    [self loadData];
}

- (void)loadData {
    
    defweakself
    [HttpRequest getURL:ktask_detail paras:@{@"type": @"task_verify", @"id": [NSString stringWithFormat:@"%d", __id]} showProgress:YES successful:^(id  _Nullable result) {
        NSLog(@"%@", result);
        weakself.info = result[@"datas"];
        [weakself setupView];
    } failed:^(int code, NSString * _Nullable errMsg) {
        
    }];
}

- (void)setupView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, XNavigationHeight, ScreenWidth, ScreenHeight-XNavigationHeight-XBotSafeSpace-40.0) collectionViewLayout:layout];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collection];
    [_collection registerNib:[UINib nibWithNibName:@"ReviewDetailCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ReviewDetailCollectionViewCell"];
    [_collection registerNib:[UINib nibWithNibName:@"ReviewDetailHeaderReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReviewDetailHeaderReusableView"];
    [_collection registerNib:[UINib nibWithNibName:@"CaptureDeviceCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CaptureDeviceCollectionViewCell"];
    
    if ([_info[@"status"] intValue] == 0) {
    
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.font = [UIFont systemFontOfSize:15.0];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitle:@"电话联系" forState:UIControlStateNormal];
        button.frame = CGRectMake(0, ScreenHeight-XBotSafeSpace-40.0, ScreenWidth*0.3, 40.0+XBotSafeSpace);
        [button addTarget:self action:@selector(call) forControlEvents:UIControlEventTouchUpInside];
        button.backgroundColor = COLOR(102, 102, 102, 1);
        [self.view addSubview:button];
        
        UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        button1.titleLabel.font = [UIFont systemFontOfSize:15.0];
        [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button1 setTitle:@"审核拒绝" forState:UIControlStateNormal];
        button1.frame = CGRectMake(ScreenWidth*0.3, ScreenHeight-XBotSafeSpace-40.0, ScreenWidth*0.25, 40.0+XBotSafeSpace);
        [button1 addTarget:self action:@selector(refuse) forControlEvents:UIControlEventTouchUpInside];
        button1.backgroundColor = COLOR(252, 13, 27, 1);
        [self.view addSubview:button1];
        
        UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
        button2.titleLabel.font = [UIFont systemFontOfSize:15.0];
        [button2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button2 setTitle:@"审核通过" forState:UIControlStateNormal];
        button2.frame = CGRectMake(ScreenWidth*0.3+ScreenWidth*0.25, ScreenHeight-XBotSafeSpace-40.0, ScreenWidth*0.45, 40.0+XBotSafeSpace);
        [button2 addTarget:self action:@selector(allow) forControlEvents:UIControlEventTouchUpInside];
        button2.backgroundColor = COLOR(56, 152, 24, 1);
        [self.view addSubview:button2];
    }
}

- (void)call {
    
    defweakself
    UIAlertController *con = [UIAlertController alertControllerWithTitle:@"电话" message:_info[@"contact_tel"] preferredStyle:UIAlertControllerStyleAlert];
    [con addAction:[UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleCancel handler:nil]];
    [con addAction:[UIAlertAction actionWithTitle:@"立即呼叫" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSString *tel = weakself.info[@"contact_tel"];
        tel = [@"telprompt://" stringByAppendingString:tel];
        if (@available(iOS 10.0, *)) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:tel] options:@{} completionHandler:nil];
        }else {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:tel]];
        }
    }]];
    [self presentViewController:con animated:YES completion:nil];
}

- (void)refuse {
    
    [Utility globalShow:self.beiZhuView];
}

- (UIView *)beiZhuView {
    
    if (!_beiZhuView) {
        
        _beiZhuView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _beiZhuView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        
        UIView *back = [[UIView alloc] initWithFrame:CGRectMake(0.0, 200.0, ScreenWidth, ScreenHeight-200.0)];
        back.backgroundColor = [UIColor whiteColor];
        [_beiZhuView addSubview:back];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 35.0)];
        label.font = [UIFont systemFontOfSize:15.0];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"拒绝说明";
        [back addSubview:label];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.font = [UIFont systemFontOfSize:15.0];
        button.frame = CGRectMake(ScreenWidth-35, 0, 35, 35);
        [button setImage:[UIImage imageNamed:@"chacha"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(hiddenBeiZhu) forControlEvents:UIControlEventTouchUpInside];
        [back addSubview:button];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0.0, 35.0, ScreenWidth, 1.0)];
        line.backgroundColor = COLOR(210, 210, 210, 1.0);
        [back addSubview:line];
        
        _textView = [[UITextView alloc] initWithFrame:CGRectMake(30.0, 40.0, ScreenWidth-60.0, 170.0)];
        _textView.layer.borderColor = COLOR(200, 200, 200, 1).CGColor;
        _textView.layer.borderWidth = 1.0;
        [back addSubview:_textView];
        
        UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
        button2.titleLabel.font = [UIFont systemFontOfSize:15.0];
        [button2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button2 setTitle:@"提交" forState:UIControlStateNormal];
        button2.frame = CGRectMake(0, back.frame.size.height-XBotSafeSpace-40.0, ScreenWidth, 40.0+XBotSafeSpace);
        [button2 addTarget:self action:@selector(toRefuse) forControlEvents:UIControlEventTouchUpInside];
        button2.backgroundColor = COLOR(56, 152, 24, 1);
        [back addSubview:button2];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(stopEditing)];
        [_beiZhuView addGestureRecognizer:tap];
    }
    
    return _beiZhuView;
}

- (void)toRefuse {
    
    if (_textView.text.length == 0) {
        return;
    }
    
    [self hiddenBeiZhu];
    
    defweakself
    [HttpRequest postURL:kreview_refuse paras:@{@"id": [NSString stringWithFormat:@"%d", __id], @"mask": _textView.text} showProgress:YES successful:^(id  _Nullable result) {
        [TipManager showTip:@"审核通过"];
        [weakself loadData];
    } failed:^(int code, NSString * _Nullable errMsg) {
        //        [TipManager showTip:@""];
    }];
}

- (void)stopEditing {
    
    [_textView endEditing:YES];
}

- (void)hiddenBeiZhu {
    
    [_beiZhuView removeFromSuperview];
}

- (void)allow {
    
    defweakself
    [HttpRequest postURL:kreview_allow paras:@{@"id": [NSString stringWithFormat:@"%d", __id]} showProgress:YES successful:^(id  _Nullable result) {
        [TipManager showTip:@"审核通过"];
        [weakself loadData];
    } failed:^(int code, NSString * _Nullable errMsg) {
//        [TipManager showTip:@""];
    }];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 2;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        if ([_info[@"status"] intValue] == -1 || [_info[@"status"] intValue] == 1) {
            
            return CGSizeMake(ScreenWidth, 100.0);
        }
    }
    
    return CGSizeZero;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        if (indexPath.section == 0) {
            
            ReviewDetailHeaderReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReviewDetailHeaderReusableView" forIndexPath:indexPath];
            if ([_info[@"status"] intValue] == 1) {
                
                view.backgroundColor = COLOR(55, 107, 250, 1);
                view.label1.text = @"已通过";
                view.label3.text = nil;
            }else if ([_info[@"status"] intValue] == -1) {
                
                view.backgroundColor = COLOR(252, 103, 61, 1);
                view.label1.text = @"已驳回";
                view.label3.text = [NSString stringWithFormat:@"%@", _info[@"mask"]];
            }
            view.label2.text = _info[@"created_at"];
            return view;
        }
    }
    return nil;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (section == 0) {
        
        if ([_info[@"verifyable_type"] isEqualToString:@"move"]) {
            return 7;
        }else if ([_info[@"verifyable_type"] isEqualToString:@"sellout"]) {
            return 5;
        }
    }else {
        
        return [(NSArray *)_info[@"autocars"] count]+1;
    }
    
    return 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return CGSizeMake(ScreenWidth, 50.0);
    }else {
        return CGSizeMake(ScreenWidth, 40.0);
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        ReviewDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ReviewDetailCollectionViewCell" forIndexPath:indexPath];
        cell.rightLabel.textColor = COLOR(51, 51, 51, 1);
        if ([_info[@"verifyable_type"] isEqualToString:@"move"]) {
            if (indexPath.item == 0) {
                cell.rightLabel.backgroundColor = COLOR(106, 202, 37, 1);
                cell.leftLabel.text = @"类型";
                cell.rightLabel.text = [NSString stringWithFormat:@" %@ ", _info[@"verifyable_type_label"]];
                cell.rightLabel.textColor = [UIColor whiteColor];
            }else if (indexPath.item == 1) {
                cell.leftLabel.text = @"所属经销商";
                cell.rightLabel.text = _info[@"dealer"][@"name"];
            }else if (indexPath.item == 2) {
                cell.leftLabel.text = @"目标停车场";
                cell.rightLabel.text = _info[@"to_parking"][@"name"];
            }else if (indexPath.item == 3) {
                cell.leftLabel.text = @"移库时间";
                cell.rightLabel.text = [NSString stringWithFormat:@"%@至%@", _info[@"start_time"], _info[@"end_time"]];
            }else if (indexPath.item == 4) {
                cell.leftLabel.text = @"移库数量";
                cell.rightLabel.text = [NSString stringWithFormat:@"%@", _info[@"autocar_nums"]];
            }else if (indexPath.item == 5) {
                cell.leftLabel.text = @"申请人";
                cell.rightLabel.text = [NSString stringWithFormat:@"%@", _info[@"apply_user"][@"name"]];
            }else if (indexPath.item == 6) {
                cell.leftLabel.text = @"车辆信息";
                cell.rightLabel.text = nil;
            }
        }else if ([_info[@"verifyable_type"] isEqualToString:@"sellout"]) {
            if (indexPath.item == 0) {
                cell.rightLabel.backgroundColor = COLOR(252, 103, 61, 1);
                cell.leftLabel.text = @"类型";
                cell.rightLabel.text = [NSString stringWithFormat:@" %@ ", _info[@"verifyable_type_label"]];
                cell.rightLabel.textColor = [UIColor whiteColor];
            }else if (indexPath.item == 1) {
                cell.leftLabel.text = @"所属经销商";
                cell.rightLabel.text = _info[@"dealer"][@"name"];
            }else if (indexPath.item == 2) {
                cell.leftLabel.text = @"关联车辆";
                cell.rightLabel.text = [NSString stringWithFormat:@"%@辆", _info[@"autocar_nums"]];
            }else if (indexPath.item == 3) {
                cell.leftLabel.text = @"申请人";
                cell.rightLabel.text = [NSString stringWithFormat:@"%@", _info[@"apply_user"][@"name"]];
            }else if (indexPath.item == 4) {
                cell.leftLabel.text = @"车辆信息";
                cell.rightLabel.text = nil;
            }
        }
        return cell;
    }else {
        
        CaptureDeviceCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CaptureDeviceCollectionViewCell" forIndexPath:indexPath];
        if (indexPath.item == 0) {
            cell.backgroundColor = COLOR(153, 153, 153, 1);
            cell.label1.text = [NSString stringWithFormat:@"芯片号"];
            cell.label3.text = [NSString stringWithFormat:@"当前停车场"];
            cell.label2.text = [NSString stringWithFormat:@"车辆名称/车架号"];
        }else {
            cell.backgroundColor = [UIColor whiteColor];
            NSDictionary *dic = _info[@"autocars"][indexPath.item-1];
            cell.label1.text = [NSString stringWithFormat:@"%@", dic[@"rfid_id"]];
            cell.label3.text = _info[@"parking"][@"name"];
            cell.label2.text = [NSString stringWithFormat:@"%@\n%@", dic[@"car_model"], dic[@"vin_no"]];
        }
        return cell;
    }
}

@end
